package com.ruoyi.order.service.impl;

import java.util.*;
import java.util.function.Function;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.Product;
import com.ruoyi.order.domain.ShoppingCar;
import com.ruoyi.order.domain.Vo.CategoryVo;
import com.ruoyi.order.domain.Vo.ProductVo;
import com.ruoyi.order.domain.app.Vo.CategoryViewVo;
import com.ruoyi.order.domain.app.Vo.CategoryResultVo;
import com.ruoyi.order.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.CategoryMapper;
import com.ruoyi.order.domain.Category;
import com.ruoyi.order.service.ICategoryService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 类别Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-02-04
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ProductMapper productMapper;

    /**
     * 查询类别
     * 
     * @param categoryID 类别主键
     * @return 类别
     */
    @Override
    public Category selectCategoryByCategoryID(Integer categoryID)
    {
        return categoryMapper.selectCategoryByCategoryID(categoryID);
    }

    /**
     * 查询类别列表
     * 
     * @param category 类别
     * @return 类别
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增类别
     * 
     * @param category 类别
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        category.setCreateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改类别
     * 
     * @param category 类别
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateTime(DateUtils.getNowDate());
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除类别
     * 
     * @param categoryIDs 需要删除的类别主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryIDs(String categoryIDs)
    {
        return categoryMapper.deleteCategoryByCategoryIDs(Convert.toStrArray(categoryIDs));
    }

    /**
     * 删除类别信息
     * 
     * @param categoryID 类别主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryID(Integer categoryID)
    {
        return categoryMapper.deleteCategoryByCategoryID(categoryID);
    }

    @Override
    public List<CategoryVo> getCategoryPage(String categoryName) {
        Map<String,Object> map = new HashMap<>();
        map.put("categoryName",categoryName);
        map.put("state",0);
        List<CategoryVo> categoryVoList = categoryMapper.getCategoryPage(map);
        return categoryVoList;
    }

    @Override
    public List<CategoryVo> getCategoryPageList() {
        Map<String,Object> map = new HashMap<>();
        map.put("state",0);
        List<CategoryVo> categoryVoList = categoryMapper.getCategoryPageList(map);
        return categoryVoList;
    }

    @Override
    public List<ProductVo> getCategoryProduct(Integer categoryID) {
        Map<String,Object> map = new HashMap<>();
        map.put("categoryID",categoryID);
        map.put("state",0);
        List<Product> products = productMapper.selectList(map);
        List<ProductVo> productVos = new ArrayList<>();
        BeanUtils.copyProperties(products,productVos);
        return productVos;
    }

    @Override
    public List<CategoryVo> getCategoryParentPage() {
        List<CategoryVo> list = categoryMapper.getCategoryParentPage();
        return list;
    }

    @Override
    public  List<CategoryResultVo> getCategoryList() {
        Map<String,Object> map = new HashMap<>();
        map.put("state",0);
        map.put("parentID",0);
        List<CategoryResultVo> resultList = categoryMapper.getCategoryInfo(map);
        if(resultList!=null&&resultList.size()>0){
            for (CategoryResultVo categoryResultVo : resultList) {
                map.clear();
                map.put("state",0);
                map.put("parentID",categoryResultVo.getCategoryId());
                categoryResultVo.setCategories(categoryMapper.getCategoryList(map));
            }
        }
        return resultList;
    }

    @Override
    public List<CategoryResultVo> getCategoryInfo(Integer parentId) {

        Map<String,Object> map = new HashMap<>();
        map.put("state",0);
        map.put("parentID",parentId);
        List<CategoryResultVo> resultList = categoryMapper.getCategoryInfo(map);
        if(resultList!=null&&resultList.size()>0){
            for (CategoryResultVo categoryResultVo : resultList) {
                map.clear();
                map.put("state",0);
                map.put("parentID",categoryResultVo.getCategoryId());
                categoryResultVo.setCategories(categoryMapper.getCategoryList(map));
            }
        }
        return resultList;
    }



    /**
     * 查询类别树列表
     *
     * @return 所有类别信息
     */
    @Override
    public List<Ztree> selectCategoryTree()
    {
        List<Category> categoryList = categoryMapper.selectCategoryList(new Category());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Category category : categoryList)
        {
            Ztree ztree = new Ztree();
            ztree.setId( category.getCategoryID());
            ztree.setpId(category.getParentID());
            ztree.setName(category.getCategoryName());
            ztree.setTitle(category.getCategoryName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public boolean saveBatch(Collection<ShoppingCar> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<ShoppingCar> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<ShoppingCar> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(ShoppingCar entity) {
        return false;
    }

    @Override
    public ShoppingCar getOne(Wrapper<ShoppingCar> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<ShoppingCar> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<ShoppingCar> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<ShoppingCar> getBaseMapper() {
        return null;
    }

    @Override
    public Class<ShoppingCar> getEntityClass() {
        return null;
    }
}
